<?php
    session_start(); 
    if(!isset($_SESSION['sessData']))
       $_SESSION['sessData'];
  include("../lib/helpers.php");
  $servers = array(1=>"192.168.1.105/startgame");
  $maxPort = 32767;
  $minPort = 10000;
  
  function getTableNum($serv)
  {
    global $minPort, $maxPort;
    
    $link = connectToDB(); 
    $sql    = "select max(tableNumber) from gameTable where server=$serv;";
    $result = mysql_query($sql, $link);
    
    print "<br/><br/>";
    if (!$result) 
    {
      print "DB Error, could not query the database\n";
      print "MySQL Error: " . mysql_error();
      return;
    }
    $row = mysql_fetch_array($result, MYSQL_NUM);
    $lastTableNum = $row[0];
    //print $lastTableNum;
    //print "<br/><br/>";
    //SQL statment is only return
    $tableNum = $lastTableNum + 1;

    $tableNumstr = strval($tableNum);
    $port =  substr($tableNumstr, -5);
    if((int)$port > $maxPort)
    {
      $servlen = strlen(strval($serv));
      //$port = "$minport";
      $uniqlen = strlen($tableNumstr) - ($servlen + 5);
      $uniq = substr($tableNumstr, $servlen, $uniqlen);
      $newuniq = (int)$uniq + 1;
      //add later if unique gains a digit.
      $servNum = substr($tableNumstr, $uniqlen, $uniqlen);
      $ret = $serv . $newuniq . $minPort;
    }
    return $ret;
  }

  function getBestServ($servarray)
  {
    $currentlow = 100;
    foreach ($servarray as $key=>$serv)
    {
      $httpresp = http_parse_message(http_get("http://" . $serv . "/getload.php"));
      $temp = (int)$httpresp->body;
      if($currentlow > $temp)
      {
        $currentlow = $temp;
        $bestServ = $key;
      }
    }
    return $bestServ;
  }

?>
<HTML>
  <head>
    <title>Starting <?php print $_GET[game]; ?></title>
  <script type="text/javascript" src="../lib/jogs.js"></script> 
  </head>
  <body>
    <?php 
    
		$bestServ = getBestServ($servers);
		print "The Best Server is " . $bestServ;

		$newTableNumstr = getTableNum($bestServ);

		print $newTableNumstr;

		print "<br/><br/>";
		print "Starting " . $_GET[game] . "<BR/>"; 
		print "with " . $_POST[players] . " players and " . $_POST[decks] . " decks<br/>"; 
		print "started by " . $_SESSION['sessData']["username"];

		print "<br/><br/>";
        
        $params = "game=" . $_GET['game'] . "&tablenum=" . $newTableNumstr;
        foreach($_POST as $key=>$value)
        {
            $params .= "&" . $key . "=" . $value;
        }

		$httpresp = http_parse_message(http_get("http://" . $servers[$bestServ] . "/launch.php?" . $params));
		print $httpresp->body;

    ?>
  <br>


  
  <div id="status">
    
  </div>
  <!--<script type="text/javascript">
    var http = getHttpReq();
    http.open("GET", "http://192.168.1.105/startgame/launch.php", true);
    http.onreadystatechange = function()
    {
      if(http.readyState == 4)// && http.status == 200)
      {
        alert(http.responseText);
        var div = document.getElementById("status");
        div.innerHTML = http.responseText;
      }
    }
    http.send(null);
  </script>-->
  </body>
</html>
  

